﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TorchView.GameContent.Data;

namespace TorchView.GameContent.Units
{
  public sealed class Item : IDataSerializable
  {
    public Item()
    {
      Wardrobes = new List<Wardrobe>(0);
      Effects = new List<Effect>(0);
      Skills = new List<UnitSkill>(0);
    }

    #region IDataSerializable Members

    public DataBlock MakeDataBlock()
    {
      throw new NotImplementedException();
    }

    public void LoadFromDataBlock(DataBlock block)
    {
      Data = block;
      Name = block.GetPropertyValue("NAME", "");
      UnitGuid = block.GetPropertyValue("UNIT_GUID", "");
      CreateAs = block.GetPropertyValue("CREATEAS", "");
      Shadows = block.GetPropertyValue("SHADOWS", false);
      DontCreate = block.GetPropertyValue("DONTCREATE", true);
      DisplayName = block.GetPropertyValue("DISPLAYNAME", "");
      UnidentifiedName = block.GetPropertyValue("UNIDENTIFIED_NAME", "");
      Description = block.GetPropertyValue("DESCRIPTION", "");
      BaseFile = block.GetPropertyValue("BASEFILE", "");
      ResourceDirectory = block.GetPropertyValue("RESOURCEDIRECTORY", "");
      MeshFile = block.GetPropertyValue("MESHFILE", "");
      SecondaryMeshFile = block.GetPropertyValue("MESHFILE_SECONDARY", "");
      TextureOverride = block.GetPropertyValue("TEXTURE_OVERRIDE", "");
      AttachedLayout = block.GetPropertyValue("ATTACHEDLAYOUT", "");
      MinDamage = block.GetPropertyValue("MINDAMAGE", 0);
      MaxDamage = block.GetPropertyValue("MAXDAMAGE", 0);
      Range = block.GetPropertyValue("RANGE", 0f);
      Speed = block.GetPropertyValue("SPEED", 0);
      StrikeRange = block.GetPropertyValue("STRIKERANGE", 0f);
      ElectricDamage = block.GetPropertyValue("DAMAGE_ELECTRIC", 0);
      IceDamage = block.GetPropertyValue("DAMAGE_ICE", 0);
      FireDamage = block.GetPropertyValue("DAMAGE_FIRE", 0);
      PoisonDamage = block.GetPropertyValue("DAMAGE_POISON", 0);
      PhysicalDamage = block.GetPropertyValue("DAMAGE_PHYSICAL", 0);
      BlockChance = block.GetPropertyValue("BLOCK_CHANCE", 0);
      UnitType = block.GetPropertyValue("UNITTYPE", "");
      Rarity = block.GetPropertyValue("RARITY", 0);
      RarityHardcore = block.GetPropertyValue("RARITY_HARDCORE", 0);
      MinLevel = block.GetPropertyValue("MINLEVEL", 0);
      MaxLevel = block.GetPropertyValue("MAXLEVEL", 0);
      Level = block.GetPropertyValue("LEVEL", 0);
      LevelRequired = block.GetPropertyValue("LEVEL_REQUIRED", 0);
      Value = block.GetPropertyValue("VALUE", 0);
      MaxStackSize = block.GetPropertyValue("MAXSTACKSIZE", 0);
      Uses = block.GetPropertyValue("USES", "");
      TargetType = block.GetPropertyValue("TARGET_TYPE", ""); //  new string[] { "", "USER", "ITEM" }
      Icon = block.GetPropertyValue("ICON", "");
      IconSheet = block.GetPropertyValue("ICON_SHEET", 0);
      ArmorMin = block.GetPropertyValue("ARMORMIN", 0);
      ArmorMax = block.GetPropertyValue("ARMORMAX", 0);
      ElectricArmor = block.GetPropertyValue("ARMOR_ELECTRIC", 0);
      IceArmor = block.GetPropertyValue("ARMOR_ICE", 0);
      FireArmor = block.GetPropertyValue("ARMOR_FIRE", 0);
      PoisonArmor = block.GetPropertyValue("ARMOR_POISON", 0);
      PhysicalArmor = block.GetPropertyValue("ARMOR_PHYSICAL", 0);
      SpeedDamageMod = block.GetPropertyValue("SPEED_DMG_MOD", 0);
      RarityDamageMod = block.GetPropertyValue("RARITY_DMG_MOD", 0);
      SpecialDamageMod = block.GetPropertyValue("SPECIAL_DMG_MOD", 0);
      RarityArmorMod = block.GetPropertyValue("RARITY_AMR_MOD", 0);
      SpecialArmorMod = block.GetPropertyValue("SPECIAL_AMR_MOD", 0);
      FallSound = block.GetPropertyValue("FALL_SOUND", "");
      LandSound = block.GetPropertyValue("LAND_SOUND", "");
      TakeSound = block.GetPropertyValue("TAKE_SOUND", "");
      UseSound = block.GetPropertyValue("USE_SOUND", "");
      AttackSound = block.GetPropertyValue("ATTACK_SOUND", "");
      StrikeSound = block.GetPropertyValue("STRIKE_SOUND", "");
      Missile = block.GetPropertyValue("MISSILE", "");
      Set = block.GetPropertyValue("SET", "");
      StrengthRequired = block.GetPropertyValue("STRENGTH_REQUIRED", 0);
      DexterityRequired = block.GetPropertyValue("DEXTERITY_REQUIRED", 0);
      MagicRequired = block.GetPropertyValue("MAGIC_REQUIRED", 0);
      DefenseRequired = block.GetPropertyValue("DEFENSE_REQUIRED", 0);
      DropArticle = block.GetPropertyValue("DROPPARTICLE", "");
      GamblerIcon = block.GetPropertyValue("GAMBLER_ICON", "");
      Sockets = block.GetPropertyValue("SOCKETS", 0);

      var affixes = block.GetSubBlock("AFFIXES");
      if (affixes != null)
        Affixes = affixes.GetProperties("AFFIX").Select(p => p.Value as string).Where(s => s != null).ToArray();

      Wardrobes.Clear();
      Wardrobes.AddRange(block.SubBlocks.Select(s =>
      {
        if (s.Name != Wardrobe.BLOCK_NAME)
          return null;
        Wardrobe w = new Wardrobe();
        w.LoadFromDataBlock(s);
        return w;
      }).Where(b => b != null));

      Effects.Clear();
      Effects.AddRange(block.SubBlocks.Select(s =>
      {
        if (s.Name != Effect.BLOCK_NAME)
          return null;
        Effect e = new Effect();
        e.LoadFromDataBlock(s);
        return e;
      }).Where(b => b != null));

      Skills.Clear();
      Skills.AddRange(block.SubBlocks.Select(s =>
      {
        if (s.Name != UnitSkill.BLOCK_NAME)
          return null;
        UnitSkill sk = new UnitSkill();
        sk.LoadFromDataBlock(s);
        return sk;
      }).Where(b => b != null));
    }

    #endregion

    public DataBlock Data { get; set; }
    public List<Wardrobe> Wardrobes { get; private set; }
    public List<Effect> Effects { get; private set; }
    public List<UnitSkill> Skills { get; private set; }

    public string Name { get; set; }
    public string UnitGuid { get; set; }
    public string DisplayName { get; set; }
    public string UnidentifiedName { get; set; }
    public string Description { get; set; }
    public string BaseFile { get; set; }
    public string ResourceDirectory { get; set; }
    public string MeshFile { get; set; }
    public string SecondaryMeshFile { get; set; }
    public string TextureOverride { get; set; }
    public string AttachedLayout { get; set; }
    public int MinDamage { get; set; }
    public int MaxDamage { get; set; }
    public float Range { get; set; }
    public int Speed { get; set; }
    public float StrikeRange { get; set; }
    public int ElectricDamage { get; set; }
    public int IceDamage { get; set; }
    public int FireDamage { get; set; }
    public int PoisonDamage { get; set; }
    public int PhysicalDamage { get; set; }
    public int BlockChance { get; set; }
    public string UnitType { get; set; }
    public int Rarity { get; set; }
    public int RarityHardcore { get; set; }
    public int MinLevel { get; set; }
    public int MaxLevel { get; set; }
    public int Level { get; set; }
    public int LevelRequired { get; set; }
    public int Value { get; set; }
    public int MaxStackSize { get; set; }
    public string Icon { get; set; }
    public string TargetType { get; set; }
    public string Uses { get; set; }
    public int ArmorMin { get; set; }
    public int ArmorMax { get; set; }
    public int ElectricArmor { get; set; }
    public int IceArmor { get; set; }
    public int FireArmor { get; set; }
    public int PoisonArmor { get; set; }
    public int PhysicalArmor { get; set; }
    public int SpeedDamageMod { get; set; }
    public int RarityDamageMod { get; set; }
    public int SpecialDamageMod { get; set; }
    public int RarityArmorMod { get; set; }
    public int SpecialArmorMod { get; set; }
    public string FallSound { get; set; }
    public string LandSound { get; set; }
    public string TakeSound { get; set; }
    public string UseSound { get; set; }
    public string AttackSound { get; set; }
    public string StrikeSound { get; set; }
    public string Missile { get; set; }
    public string Set { get; set; }
    public int StrengthRequired { get; set; }
    public int DexterityRequired { get; set; }
    public int MagicRequired { get; set; }
    public int DefenseRequired { get; set; }
    public string DropArticle { get; set; }
    public string GamblerIcon { get; set; }
    public string[] Affixes { get; set; }
    public string CreateAs { get; set; }
    public bool Shadows { get; set; }
    public bool DontCreate { get; set; }
    public int Sockets { get; set; }
    public int IconSheet { get; set; }
  }
}
